package com.jing.myservice;
//

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.jing.myservice.service.MyBinderService;
import com.jing.myservice.service.MyComplexService;
import com.jing.myservice.service.MyService;


//public class MainActivity extends AppCompatActivity {
//
//    private MyBoundService myBoundService;
//    private boolean isBound = false;
//
//    private ServiceConnection connection = new ServiceConnection() {
//        @Override
//        public void onServiceConnected(ComponentName className, IBinder service) {
//            MyBoundService.LocalBinder binder = (MyBoundService.LocalBinder) service;
//            myBoundService = binder.getService();
//            isBound = true;
//            Log.d("MainActivity", "Service Connected: " + myBoundService.getServiceData());
//        }
//
//        @Override
//        public void onServiceDisconnected(ComponentName arg0) {
//            isBound = false;
//        }
//    };
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
//
////        Intent intent = new Intent(this, MyService.class);
////        startService(intent);
//
//
//        Intent intent = new Intent(this, MyBoundService.class);
//        bindService(intent, connection, Context.BIND_AUTO_CREATE);
//
//        Handler handler = new Handler();
//        handler.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                myBoundService.setServiceData("Service finish");
//                Log.d("MainActivity", "Service getLogData: " + myBoundService.getServiceData());
//                // 执行延时后的UI更新任务
//                System.exit(0);
//            }
//        }, 5000); // 延时5秒
//
//
//    }
//
//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        if (isBound) {
//            unbindService(connection);
//            isBound = false;
//        }
//    }
//}

//
//public class MainActivity extends AppCompatActivity {
//    private static final String TAG = "MainActivity";
//    private Messenger serviceMessenger;
//    private boolean isBound = false;
//    private TextView textView;
//
//    private final Messenger clientMessenger = new Messenger(new IncomingHandler());
//
//    private final ServiceConnection connection = new ServiceConnection() {
//        @Override
//        public void onServiceConnected(ComponentName className, IBinder service) {
//            serviceMessenger = new Messenger(service);
//            isBound = true;
//
//            Message msg = Message.obtain(null, 3);
//            msg.replyTo = clientMessenger;
//            try {
//                serviceMessenger.send(msg);
//            } catch (RemoteException e) {
//                e.printStackTrace();
//            }
//        }
//
//        @Override
//        public void onServiceDisconnected(ComponentName arg0) {
//            serviceMessenger = null;
//            isBound = false;
//        }
//    };
//
//    private class IncomingHandler extends Handler {
//        @Override
//        public void handleMessage(Message msg) {
//            int count = msg.getData().getInt("count");
//            textView.setText("Count: " + count);
//        }
//    }
//
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
//        textView = findViewById(R.id.textView);
//
//        Intent intent = new Intent(this, MyComplexService.class);
//        bindService(intent, connection, Context.BIND_AUTO_CREATE);
//
//        findViewById(R.id.startButton).setOnClickListener(v -> startServiceTask());
//        findViewById(R.id.stopButton).setOnClickListener(v -> stopServiceTask());
//    }
//
//    private void startServiceTask() {
//        if (isBound) {
//            Message msg = Message.obtain(null, 1);
//            try {
//                serviceMessenger.send(msg);
//            } catch (RemoteException e) {
//                e.printStackTrace();
//            }
//        }
//    }
//
//    private void stopServiceTask() {
//        if (isBound) {
//            Message msg = Message.obtain(null, 2);
//            try {
//                serviceMessenger.send(msg);
//            } catch (RemoteException e) {
//                e.printStackTrace();
//            }
//        }
//    }
//
//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        if (isBound) {
//            unbindService(connection);
//            isBound = false;
//        }
//    }
//}

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private IMyAidlInterface mService;
    private boolean isBound = false;
    private TextView textView;

    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName className, IBinder service) {
            mService = IMyAidlInterface.Stub.asInterface(service);
            isBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            mService = null;
            isBound = false;
        }
    };

    private Handler handler;
    private Runnable runnable;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.textView);

        Intent intent = new Intent(this, MyBinderService.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

        Button startButton = findViewById(R.id.startButton);
        Button stopButton = findViewById(R.id.stopButton);

        startButton.setOnClickListener(v -> startServiceTask());
        stopButton.setOnClickListener(v -> stopServiceTask());

         handler = new Handler(Looper.getMainLooper());
         runnable = new Runnable() {
            @Override
            public void run() {
                int count = 0;
                try {
                    count = mService.getCount();
                    Log.d(TAG, "run: count-->"+count);
                } catch (Exception e) {
                   e.printStackTrace();
                }
                textView.setText("Count: " + count);
                handler.postDelayed(this, 1000);
            }
        };

    }

    private void startServiceTask() {
        if (isBound) {
            Log.d(TAG, "Start button clicked");
            try {
                mService.startTask();
                handler.removeCallbacks(runnable);
                handler.post(runnable);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    private void stopServiceTask() {
        if (isBound) {
            Log.d(TAG, "Stop button clicked");
            try {
                mService.stopTask();
                handler.removeCallbacks(runnable);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (isBound) {
            unbindService(mConnection);
            isBound = false;
        }
    }
}